package com.glsc.ngateway.platform.service.costallocation;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.glsc.ngateway.common.api.ldap.dto.LdapEhrDeptDto;
import com.glsc.ngateway.common.api.ldap.dto.LdapEhrEmpDto;
import com.glsc.ngateway.common.api.ldap.feign.IFeignLdapService;
import com.glsc.ngateway.common.api.ldap.resp.AppEhrResponse;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.User;
import com.glsc.ngateway.platform.domain.costallocation.DepCostAllocation;
import com.glsc.ngateway.platform.repo.costallocation.DepCostAllocationRepo;
import com.glsc.ngateway.platform.service.UserService;
import com.glsc.ngateway.platform.utils.SpecificationUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author guoj
 * @description: 成分分摊比例
 * @date 2024/7/19 9:46
 */
@Service
public class DepCostAllocationService {
    @Resource
    private DepCostAllocationRepo depCostAllocationRepo;
    @Resource
    IFeignLdapService feignLdapService;
    @Resource
    private UserService userService;

    /**
     * 查询
     * @param systemIdSet
     * @return
     */
    public Map<Long, List<DepCostAllocation>> queryBySystemIdSet(Set<Long> systemIdSet,Map<String, Object> paramCostAllocation){
        if (systemIdSet == null || systemIdSet.size() == 0) {
            return new HashMap<>();
        }
        List<DepCostAllocation> toGroup;
        List<DepCostAllocation> depCostAllocations = depCostAllocationRepo.queryBySystemInfoId(systemIdSet);
        if (paramCostAllocation.size() != 0) {
            List<DepCostAllocation> all = depCostAllocationRepo.findAll(SpecificationUtil.buildSpecification(paramCostAllocation, DepCostAllocation.class));
            if(CollectionUtil.isNotEmpty(all)){
                Set<Long> collect = all.stream().map(DepCostAllocation::getSystemInfoId).collect(Collectors.toSet());
                //取交集
                systemIdSet.retainAll(collect);
                toGroup = depCostAllocations.stream().filter(x -> systemIdSet.contains(x.getSystemInfoId())).collect(Collectors.toList());
            }else{
                toGroup = new ArrayList<>();
            }
        }else{
            //此时子类无查询字段  按照父类查询
            toGroup = depCostAllocations;
        }
      //toGroup = setGradeOneTwoNameAndLeaderName(toGroup);
      Map<Long, List<DepCostAllocation>> systemInfoId2List = toGroup.stream()
                .collect(Collectors.groupingBy(DepCostAllocation::getSystemInfoId));
        return systemInfoId2List;
    }

    public List<DepCostAllocation> setGradeOneTwoNameAndLeaderName(List<DepCostAllocation> source){
        if (source == null || source.size() == 0) {
            return source;
        }
        //获取所有部门
        AppEhrResponse<List<LdapEhrDeptDto>> depts = feignLdapService.getAllDept();
        //过滤无效
        depts.setData(depts.getData().stream().filter(u -> !"1".equals(u.getCanceled())).collect(Collectors.toList()));
        List<LdapEhrDeptDto> data = depts.getData();
        if (Objects.isNull(depts) || CollectionUtil.isEmpty(data)) {
            return new ArrayList<>();
        }

        Map<Integer, String> depId2Depname = data.stream()
                .filter(x ->
                {
                    return "一级部门".equals(x.getDepgrade())
                            || "二级部门".equals(x.getDepgrade());
                }).collect(Collectors.toMap(LdapEhrDeptDto::getDepid, LdapEhrDeptDto::getDtitle));
        depId2Depname.put(20241101,"国联证券全部门");
        Map<String, String> oaAccount2Name = oaAccount2Name(source);
        for (DepCostAllocation temp : source) {
            String depGradeOneId = temp.getDepGradeOneId();
            if (!StrUtil.isEmpty(depGradeOneId)) {
                temp.setDepGradeOneName(depId2Depname.get(Integer.parseInt(depGradeOneId)));
            }

            String depLeader = temp.getDepLeader();
            //拼接一个或多个全部责任人
            if(!StrUtil.isEmpty(depLeader)){
                if(depLeader.contains(",")){
                    String[] split = depLeader.split(",");
                    List<String> oaaccounts = Arrays.asList(split);
                    List<String> userNameList = new ArrayList<>();
                    for(String oaaccount : oaaccounts){
                        String userNameTemp = oaAccount2Name.get(oaaccount);
                        if(StrUtil.isEmpty(userNameTemp)){
                            continue;
                        }
                        userNameList.add(userNameTemp);
                    }
                    temp.setDepLeaderName(StringUtils.join(userNameList, ","));
                }else{
                    temp.setDepLeaderName(oaAccount2Name.get(depLeader));
                }
            }

        }
        return source;
    }


    /**
     * oa账户到姓名
     * @param content
     * @return
     */
    private Map<String,String> oaAccount2Name(List<DepCostAllocation> content){
        Set<String> depLeaderUserIdSet = content.stream()
                .filter(x -> Objects.nonNull(x.getDepLeader()))
                .map(DepCostAllocation::getDepLeader)
                .collect(Collectors.toSet());

        List<String> depLeaderUserUserIdList = new ArrayList<>();
        for (String systemUserIdTemp : depLeaderUserIdSet) {
            if(StrUtil.isEmpty(systemUserIdTemp)){
                continue;
            }
            //多个责任人
            if(systemUserIdTemp.contains(",")){
                String[] split = systemUserIdTemp.split(",");
                depLeaderUserUserIdList.addAll(Arrays.asList(split));
            }else{
                depLeaderUserUserIdList.add(systemUserIdTemp);
            }
        }

        AppEhrResponse<List<LdapEhrEmpDto>> userListByUserLeaderAccountList = feignLdapService.getEmpsByAccounts(depLeaderUserUserIdList);
        List<LdapEhrEmpDto> data = userListByUserLeaderAccountList.getData();

        return data
                .stream()
                .collect(Collectors.toMap(LdapEhrEmpDto::getBadge, LdapEhrEmpDto::getName));
    }


}
